package com.atom.process.cnmuseum.bm.dao.task;


import com.atom.common.dao.AvensCommonDao;
import com.atom.common.exception.AvensNullPointerException;
import com.atom.common.mb.entity.gl.WhsxDO;
import com.atom.common.remote.user.UserUtil;
import com.atom.process.cnmuseum.bm.entity.task.BmWhsxTaskDO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BmWhsxTaskDao extends AvensCommonDao<BmWhsxTaskDO> {

    public List<BmWhsxTaskDO> findByTaskId(String taskId) {
        List<BmWhsxTaskDO> list = super.findByProperty("taskId", taskId);
        return list;
    }

    public BmWhsxTaskDO findLastByProcessId(String taskId) {
        List<BmWhsxTaskDO> list = super.findByPropertyOrderBy("processId", taskId, "id");
        if (list == null || list.size() == 0) {
            throw new AvensNullPointerException("TaskDO is null where taskId = " + taskId);
        } else {
            return list.get(list.size() - 1);
        }
    }


    public void createADO(UserUtil user, String processInstanceId, String taskId, WhsxDO whsx) {
        BmWhsxTaskDO ado = new BmWhsxTaskDO();
        BeanUtils.copyProperties(whsx, ado, "id");
        ado.setProcessId(processInstanceId);
        ado.setTaskId(taskId);
        save(ado);
    }

}
